package com.warmlight.voicepacket.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.warmlight.voicepacket.R;

public class AlertFragmentDialog extends AppCompatDialogFragment {

    private ClickLeftRight clickLeftRight;
    private String title;
    private String msg;
    private String left;
    private String right;

    public interface ClickLeftRight{
        void onClick(int witch);
    }

    public void setClickLeftRight(ClickLeftRight clickLeftRight){
        this.clickLeftRight = clickLeftRight;
    }

    public static AlertFragmentDialog newInstance(String title, String msg,boolean cancleble,ClickLeftRight clickLeftRight) {
        AlertFragmentDialog f = new AlertFragmentDialog();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("msg", msg);
        f.setArguments(args);

        f.setCancelable(cancleble);
        f.setClickLeftRight(clickLeftRight);

        return f;
    }
    public static AlertFragmentDialog newInstance(String title, String msg,String left,String right,boolean cancleble,ClickLeftRight clickLeftRight) {
        AlertFragmentDialog f = new AlertFragmentDialog();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("msg", msg);
        args.putString("left", left);
        args.putString("right", right);
        f.setArguments(args);

        f.setCancelable(cancleble);
        f.setClickLeftRight(clickLeftRight);

        return f;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final Dialog dialog = new Dialog(getActivity(), R.style.NormalDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_fragment_alert);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消
        Bundle arguments = getArguments();
        if (arguments != null){
            title = arguments.getString("title");
            msg = arguments.getString("msg");
            left = arguments.getString("left","取消");
            right = arguments.getString("right","确定");

        }

        TextView tv_title = (TextView) dialog.findViewById(R.id.tv_title);
        TextView tv_msg = (TextView) dialog.findViewById(R.id.tv_msg);
        TextView tv_cancle = (TextView) dialog.findViewById(R.id.tv_cancle);
        TextView tv_yes = (TextView) dialog.findViewById(R.id.tv_yes);

        tv_title.setText(title);
        tv_msg.setText(msg);

        tv_cancle.setText(left);
        tv_yes.setText(right);

        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickLeftRight!=null){
                    clickLeftRight.onClick(0);
                }
                dismiss();
            }
        });

        tv_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickLeftRight!=null){
                    clickLeftRight.onClick(1);
                }
                dismiss();
            }
        });

        return dialog;
    }

}
